<?php

if(!defined('DIR_UPLOAD')) {
	define('DIR_UPLOAD', DIR_WWW.'upload'.DIR_SEP);
	if(!is_dir(DIR_UPLOAD))
		mkdir(DIR_UPLOAD);
}

require_once(DIR_LIB."VsCore".DIR_SEP."vsfile.php");

class VsUpload {

	private $_dirUpload;

	private $_postFile = NULL;
	private $_file = NULL; // Tova e ve4e uploadnitiq file path relativno na DOCUMENT_ROOT

	private $_uploaded = FALSE;	// uploaded ili ne
	private $_error = NULL;	// Error Message

	function VsUpload($postFile, $dirUpload = DIR_UPLOAD) {
		$this->_postFile = $postFile;

		$this->_dirUpload = $dirUpload;
	}
	//====================================================================================

	function upload() {
		$uniqName = $this->makeUniqFileName();
		$uploadfile = &new VsFile($this->_dirUpload.$uniqName);

		$this->_uploaded = move_uploaded_file($this->_postFile['tmp_name'], $uploadfile->path());

		//echo "Uniq Name:	&nbsp;&nbsp;".$uniqName."<br />";
		//echo "Tmp Name:	&nbsp;&nbsp;".$this->_postFile['tmp_name']."<br />";
		//echo "Upload Name:	&nbsp;&nbsp;".$uploadfile."<br />";

		if($this->_uploaded) {
			//$this->_file = "product_images/".$uniqName;
			$this->_file = $uploadfile->wwwPath();
		}
		else {
			$this->_error = "Unable to upload this file.";
		}
			
		return $this->_uploaded; 
	}
	//=====================================================================================

	function isUploaded() { return $this->_uploaded; }
	function error() { return $this->_error; }
	function file() { return $this->_file; }
	//=====================================================================================

	private function makeUniqFileName() {
		$userFile =explode(".", $this->_postFile['name']);
		$ext = strtolower($userFile[count($userFile)-1]);

		$uniqName = md5(date("Y-m-d H:i:s")).".".$ext;

		return $uniqName;
	}
	//====================================================================================

};

?>
